{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Sum of Four Digit Number After Splitting Digits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #拆分数位后四位数字的最小和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个四位&nbsp;<strong>正</strong>&nbsp;整数&nbsp;<code>num</code>&nbsp;。请你使用 <code>num</code>&nbsp;中的 <strong>数位</strong> ，将&nbsp;<code>num</code>&nbsp;拆成两个新的整数&nbsp;<code>new1</code>&nbsp;和&nbsp;<code>new2</code>&nbsp;。<code>new1</code> 和&nbsp;<code>new2</code>&nbsp;中可以有&nbsp;<strong>前导 0</strong>&nbsp;，且&nbsp;<code>num</code>&nbsp;中 <strong>所有</strong>&nbsp;数位都必须使用。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>比方说，给你&nbsp;<code>num = 2932</code>&nbsp;，你拥有的数位包括：两个&nbsp;<code>2</code>&nbsp;，一个&nbsp;<code>9</code>&nbsp;和一个&nbsp;<code>3</code>&nbsp;。一些可能的&nbsp;<code>[new1, new2]</code>&nbsp;数对为&nbsp;<code>[22, 93]</code>，<code>[23, 92]</code>，<code>[223, 9]</code> 和&nbsp;<code>[2, 329]</code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回可以得到的&nbsp;<code>new1</code>&nbsp;和 <code>new2</code>&nbsp;的 <strong>最小</strong>&nbsp;和。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>num = 2932\n",
    "<b>输出：</b>52\n",
    "<b>解释：</b>可行的 [new1, new2] 数对为 [29, 23] ，[223, 9] 等等。\n",
    "最小和为数对 [29, 23] 的和：29 + 23 = 52 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>num = 4009\n",
    "<b>输出：</b>13\n",
    "<b>解释：</b>可行的 [new1, new2] 数对为 [0, 49] ，[490, 0] 等等。\n",
    "最小和为数对 [4, 9] 的和：4 + 9 = 13 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1000 &lt;= num &lt;= 9999</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-sum-of-four-digit-number-after-splitting-digits](https://leetcode.cn/problems/minimum-sum-of-four-digit-number-after-splitting-digits/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-sum-of-four-digit-number-after-splitting-digits](https://leetcode.cn/problems/minimum-sum-of-four-digit-number-after-splitting-digits/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['2932', '4009']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        a=[]\n",
    "        while num:\n",
    "            a.append(num%10)\n",
    "            num//=10\n",
    "        a.sort()\n",
    "        return a[0]*10+a[-1]+a[1]*10+a[2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def minimumSum(self, num):\n",
    "        lst = sorted(map(int, str(num)))\n",
    "        return (lst[0] + lst[1]) * 10 + lst[2] + lst[3]      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        a = sorted([int(i) for i in str(num)])\n",
    "        return a[0]*10+a[1]*10+a[2]+a[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = list()\n",
    "        while num!=0:\n",
    "            nums.append(num%10)\n",
    "            num = num//10\n",
    "        nums.sort()\n",
    "        return (nums[0]+nums[1])*10 + nums[2] + nums[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = \"\".join(sorted(list(str(num))))\n",
    "        print(nums)\n",
    "        a, b =0, 0\n",
    "        for i, k in enumerate(nums):\n",
    "            if i % 2 == 0:\n",
    "                a = a*10 + int(k)\n",
    "            else:\n",
    "                b = b*10 + int(k)\n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = sorted(list(str(num)))\n",
    "        return int(nums[0]+nums[3])+int(nums[1]+nums[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        ans = []\n",
    "        for i in range(4):\n",
    "            ans.append(num%10)\n",
    "            num //= 10\n",
    "        ans.sort()\n",
    "        return 10*(ans[0] + ans[1]) + ans[2] + ans[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits=[]\n",
    "        while num>0:\n",
    "            digits.append(num%10)\n",
    "            num//=10\n",
    "        digits.sort()\n",
    "        return 10*(digits[0]+digits[1])+digits[2]+digits[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits = []\n",
    "        while num:\n",
    "            digits.append(num % 10)\n",
    "            num //= 10\n",
    "        digits.sort()\n",
    "        return 10 * (digits[0] + digits[1]) + digits[2] + digits[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, a: int) -> int:\n",
    "        a = [*str(a)]\n",
    "        b = [int(i) for i in a]\n",
    "        b.sort()\n",
    "        return (b[0]*10+b[3]+b[1]*10+b[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits = []\n",
    "        while num:\n",
    "            digits.append(num % 10)\n",
    "            num //= 10\n",
    "        digits.sort()\n",
    "        return 10 * (digits[0] + digits[1]) + digits[2] + digits[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        str_num = sorted(str(num))        \n",
    "        return int(str_num[0] + str_num[-1]) + int(''.join(str_num[1:3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        list = []\n",
    "        while num != 0:\n",
    "            list.append(num % 10)\n",
    "            num = math.floor(num / 10)\n",
    "        list.sort(reverse=True)\n",
    "        numa = 0\n",
    "        numb = 0\n",
    "        for i in range(0, len(list)):\n",
    "            if list[i] == 0:\n",
    "                continue\n",
    "            if numa == 0:\n",
    "                numa = list[i]\n",
    "            elif numb == 0:\n",
    "                numb = list[i]\n",
    "            elif numa < 10:\n",
    "                numa = list[i] * 10 + numa\n",
    "            else:\n",
    "                numb = list[i] * 10 + numb\n",
    "        return numa + numb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits=[]\n",
    "        while num:\n",
    "            digits.append(num%10)\n",
    "            num//=10\n",
    "        digits.sort()\n",
    "        return 10*(digits[0]+digits[1])+digits[2]+digits[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num_list = list(str(num))\n",
    "        num_list.sort()\n",
    "        return int(num_list[0] + num_list[2]) + int(num_list[1] + num_list[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = sorted(list(str(num)))\n",
    "        return int(num[0]+num[2])+int(num[1]+num[3])\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = str(num)\n",
    "        n_lst = sorted(nums)\n",
    "        num1 = int(n_lst[0] + n_lst[2])\n",
    "        num2 = int(n_lst[1] + n_lst[3])\n",
    "        return num1 + num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        temp=sorted(list(str(num)))\n",
    "        res=int(temp[0])*10+int(temp[-1])+int(temp[1])*10+int(temp[-2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        nums = []\n",
    "        for ch in s:\n",
    "            nums.append(int(ch))\n",
    "        nums.sort()\n",
    "        if nums[0] == 0 and nums[1] == 0:\n",
    "            return nums[2] + nums[3]\n",
    "        elif nums[0] == 0:\n",
    "            return nums[1] * 10 + nums[2] + nums[3]\n",
    "        else:\n",
    "            return nums[0] * 10 + nums[3] + nums[1] * 10 + nums[2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums=list(str(num))\n",
    "        nums.sort()\n",
    "        a=(int(nums[0])+int(nums[1]))*10\n",
    "        b=int(nums[2])+int(nums[3])\n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits = []\n",
    "        while num:\n",
    "            digits.append(num % 10)\n",
    "            num //= 10\n",
    "        digits.sort()\n",
    "        return 10 * (digits[0] + digits[1]) + digits[2] + digits[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        z = list(str(num))\n",
    "        z.sort()\n",
    "        return int(z[0]+z[2])+int(z[1]+z[3])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        arr = sorted(map(int, list(str(num))))\n",
    "        return arr[0] * 10 + arr[2] + arr[1] * 10 + arr[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num_list = sorted([num // 1000, num // 100 % 10, num // 10 % 10, num % 10])\n",
    "        return num_list[0] * 10 + num_list[1] * 10 + num_list[2] + num_list[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        n4=int(str(num)[0])\n",
    "        n3=int(str(num)[1])\n",
    "        n2=int(str(num)[2])\n",
    "        n1=int(str(num)[3])\n",
    "        list1=[]\n",
    "        list1.extend([n4,n3,n2,n1])\n",
    "        list1.sort()\n",
    "        return 10*(list1[0]+list1[1])+list1[2]+list1[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = list(str(num))\n",
    "        nums.sort()\n",
    "        new1 = int(nums[0])*10+int(nums[-1])\n",
    "        new2 = int(nums[1])*10+int(nums[-2])\n",
    "        return new1+new2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        #num = 2932\n",
    "        num = sorted(str(num))\n",
    "        #2239\n",
    "        #2349\n",
    "        #2579\n",
    "        return int(num[0]+num[2])+ int(num[1]+num[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        n=str(num)\n",
    "        x=sorted([i for i in n])\n",
    "        a=''\n",
    "        b=''\n",
    "        for i in range(0,len(x),2):\n",
    "            a+=x[i]\n",
    "        for i in range(1,len(x),2):\n",
    "            b+=x[i]\n",
    "        return int(a)+int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "      sorted_num = sorted(str(num))\n",
    "      operand1 = sorted_num[0] +  sorted_num[3]\n",
    "      operand2 = sorted_num[1] +  sorted_num[2]\n",
    "\n",
    "      return int(operand1) + int(operand2)\n",
    "      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        str_num = sorted(str(num))\n",
    "        \n",
    "        return int(str_num[0] + str_num[-1]) + int(''.join(str_num[1:3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        list1 = []\n",
    "        while num != 0:\n",
    "            list1.append(num%10)\n",
    "            num //= 10\n",
    "        list1.sort()\n",
    "        return list1[0]*10+list1[3]+list1[1]*10+list1[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        s = list(s)\n",
    "        s.sort()\n",
    "        a=[]\n",
    "        b=[]\n",
    "        for i in range(len(s)):\n",
    "            if i%2==0:\n",
    "                a.append(s[i])\n",
    "            else:\n",
    "                b.append(s[i])\n",
    "        a= ''.join(a)\n",
    "        b=''.join(b)\n",
    "        return int(a)+int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = list(str(num))\n",
    "        num.sort()\n",
    "        return int(num[0]+num[2])+int(num[1]+num[3])\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        l = sorted(list(str(num)))\n",
    "        return int('{}{}'.format(l[0], l[-1])) + int('{}{}'.format(l[1], l[-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        l=[]\n",
    "        while num:\n",
    "            num,n=divmod(num,10)\n",
    "            l.append(n)\n",
    "        l.sort()\n",
    "        return 10*(l[0]+l[1])+l[2]+l[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        n = sorted([int(i) for i in str(num)]) #一共就4位\n",
    "        return 10*(n[0]+n[1]) + n[2] + n[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = sorted(list(str(num)))\n",
    "        return int('{}{}'.format(nums[0],nums[-1]))+int('{}{}'.format(nums[1],nums[-2]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        res = []\n",
    "        for i in list(str(num)):\n",
    "            res.append(int(i))\n",
    "        res.sort()\n",
    "        return (res[0]+res[1])*10+res[2]+res[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        a = []\n",
    "        for i in str(num):\n",
    "            a.append(i)\n",
    "        a.sort()\n",
    "        num1 = a[0]+a[2]\n",
    "        num2 = a[1]+a[3]\n",
    "        return int(num1)+int(num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = sorted(str(num))\n",
    "        return int(num[0]+num[2])+int(num[1]+num[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        n = sorted([int(i)for i in str(num)])\n",
    "        return 10*(n[0]+n[1]) + n[2] + n[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        list = []\n",
    "        while(num):\n",
    "            list.append(num % 10)\n",
    "            num = num // 10\n",
    "        list.sort()\n",
    "        return (list[0] + list[1])*10 + list[2] + list[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = sorted(list(str(num)))\n",
    "        s1, s2 = str(), str()\n",
    "        for x in range(len(num)):\n",
    "            if x % 2 == 0:\n",
    "                s1 += num[x]\n",
    "            else:\n",
    "                s2 += num[x]\n",
    "        return int(s1)+int(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        ans = []\n",
    "        while num:\n",
    "            ans.append(num % 10)\n",
    "            num //= 10\n",
    "        ans.sort()\n",
    "\n",
    "        return (ans[0] + ans[1]) * 10 + ans[2] + ans[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        \n",
    "        self.num = str(num)\n",
    "        self.list = list(self.num)\n",
    "        self.list = [int(i) for i in self.list]\n",
    "\n",
    "        self.count_zero = 0\n",
    "        self.no_zero = []\n",
    "        for i in self.list:\n",
    "            if i == 0:\n",
    "                self.count_zero += 1\n",
    "            elif i!= 0:\n",
    "                self.no_zero.append(i)\n",
    "\n",
    "        if self.count_zero == 1:\n",
    "            min_number = min(self.no_zero)\n",
    "            self.no_zero.remove(min_number)\n",
    "\n",
    "            return 10*min_number+sum(self.no_zero)\n",
    "        elif self.count_zero >= 2:\n",
    "\n",
    "            return  sum(self.no_zero)\n",
    "\n",
    "        elif self.count_zero == 0:\n",
    "            min_1 = min(self.no_zero)\n",
    "            self.no_zero.remove(min_1)\n",
    "            min_2 = min(self.no_zero)\n",
    "            self.no_zero.remove(min_2)\n",
    "            return 10*min_1 + 10*min_2 + sum(self.no_zero)\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        list = [0, 0, 0, 0]\n",
    "        list[0] = num // 1000\n",
    "        num -= list[0] * 1000\n",
    "        list[1] = num // 100\n",
    "        num -= list[1] * 100\n",
    "        list[2] = num // 10\n",
    "        list[3] = num - list[2] * 10\n",
    "\n",
    "        print(list)\n",
    "        list.sort()\n",
    "\n",
    "        return list[0] * 10 + list[1] * 10 + list[2] + list[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        a, b, c, d = map(int, sorted(str(num)))\n",
    "        return (a + b) * 10 + c + d\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        s = [i for i in str(num)]\n",
    "        s.sort()\n",
    "        return int(s[0] + s[3]) + int(s[1] + s[2])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        num1 = sorted(list(s))\n",
    "        return int(num1[0] + num1[3]) + int(num1[1] + num1[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = sorted([int(s) for s in str(num)])\n",
    "        return num[0]*10+num[3] + num[1]*10+num[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        minx=math.inf\n",
    "        s=str(num)        \n",
    "        res=[]\n",
    "        for i in range(len(s)):\n",
    "            t=s[:i]+s[i+1:]\n",
    "            for m in range(len(t)):\n",
    "                t1=t[:m]+t[m+1:]\n",
    "                for n in range(len(t1)):\n",
    "                    t2=t1[:n]+t1[n+1:]\n",
    "                    for k in range(len(t2)):\n",
    "                        r=t[m]+t1[n]+t2[k]\n",
    "                        res.append([s[i],r])\n",
    "                        res.append([s[i]+t[m],t1[n]+t2[k]])        \n",
    "        #print(res)\n",
    "        for i in (res):\n",
    "            minx=min(minx,abs(int(i[0])+int(i[1])))\n",
    "        return minx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num_list = list(str(num))\n",
    "        num_list.sort()  # high time cost\n",
    "        return int(num_list[0] + num_list[2]) + int(num_list[1] + num_list[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num_list = list(str(num))\n",
    "        num_list.sort()\n",
    "        return int(num_list[0] + num_list[2]) + int(num_list[1] + num_list[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = sorted(list(str(num)))\n",
    "        a = int(nums[0]) * 10 + int(nums[2])\n",
    "        b = int(nums[1]) * 10 + int(nums[3])\n",
    "        return a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = list(str(num))\n",
    "        num.sort()\n",
    "        return int(num[0]+num[2]) + int(num[1]+num[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        data_list = list(str(num))\n",
    "        n = len(data_list)\n",
    "        data_list = sorted(data_list)\n",
    "        data_min = min(data_list[0] + data_list[2], data_list[2] + data_list[0])\n",
    "        data_min2 = min(data_list[1] + data_list[3], data_list[3] + data_list[1])\n",
    "        return int(data_min) + int(data_min2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        lst=[]\n",
    "        for x in str(num):\n",
    "            lst.append(x)\n",
    "        lst.sort()\n",
    "        return int(lst[0]+lst[3])+int(lst[1]+lst[2])\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        res=[]\n",
    "        while num:\n",
    "            res.append(num%10)\n",
    "            num //=10\n",
    "        res.sort()\n",
    "        print(res)\n",
    "        return res[2]+res[3]+(res[0]+res[1])*10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        an=sorted(map(int,str(num)))\n",
    "        return (an[0]+an[1])*10+an[2]+an[3]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        d=[num%100//10,num%1000//100,num//1000,num%10]\n",
    "        for i in range(4):\n",
    "            for j in range(i,4):\n",
    "                if d[i]>d[j]:\n",
    "                    t=d[i]\n",
    "                    d[i]=d[j]\n",
    "                    d[j]=t\n",
    "\n",
    "        return 10*(d[0]+d[1])+d[2]+d[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        l = sorted(str(num))\n",
    "        nums = int(l[0])*10 + int(l[2])+ int(l[1])*10 + int(l[3]) \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def minimumSum(self, num):\n",
    "        lst = sorted(map(int, str(num)))\n",
    "        return (lst[0] + lst[1]) * 10 + lst[2] + lst[3]      \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits = list(str(num))\n",
    "        digits.sort()\n",
    "        return (int(digits[0]) + int(digits[1])) * 10 + int(digits[2]) + int(digits[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        str1 = ''\n",
    "        str2 = ''\n",
    "        s = sorted(str(num))\n",
    "        for i in range(0,len(s)):\n",
    "            if i % 2 == 0:\n",
    "                str1 += s[i]\n",
    "            else:\n",
    "                str2 += s[i]\n",
    "        return (int(str1)+int(str2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        lis =sorted([int(i) for i in list(str(num))])\n",
    "        return (lis[0]+lis[1])*10+lis[2]+lis[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        tmp = list(str(num))\n",
    "        tmp = [int(x) for x in tmp]\n",
    "        tmp = sorted(tmp)\n",
    "        num1 = str(tmp[0]) + str(tmp[2])\n",
    "        num2 = str(tmp[1]) + str(tmp[3])\n",
    "        return int(num1)+int(num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        strNum=str(num)\n",
    "        l=[]\n",
    "        for i in range(4):\n",
    "            l.append(int(strNum[i]))\n",
    "        l.sort()\n",
    "        return l[0]*10+l[2]+l[1]*10+l[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        q=[]\n",
    "        for i in range(4):\n",
    "            q.append(num%10)\n",
    "            num//=10\n",
    "        q.sort()\n",
    "        return q[0]*10 + q[3] + q[1]*10 + q[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        \n",
    "        lst = list(str(num))\n",
    "        ans = inf\n",
    "        m = len(lst)\n",
    "        for item in permutations(lst, m):\n",
    "            tmp = list(item)\n",
    "            for x in range(1, m):\n",
    "                a = \"\".join(tmp[:x])\n",
    "                b = \"\".join(tmp[x:])\n",
    "                if int(a)+int(b) < ans:\n",
    "                    ans = int(a)+int(b)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        ls = []\n",
    "        while num :\n",
    "            ele = num % 10\n",
    "            num = num // 10\n",
    "            ls.append(ele)\n",
    "        ls.sort()\n",
    "        num1 = ls[0] * 10 + ls[2]\n",
    "        num2 = ls[1] * 10 + ls[3]\n",
    "        return num1 + num2\n",
    "'''\n",
    "'''\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        ls = list(str(num))\n",
    "        ls.sort()\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        nums = sorted(list(str(num)))\n",
    "        return int('{}{}'.format(nums[0],nums[-1]))+int('{}{}'.format(nums[1],nums[-2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        num = [int(c) for c in str(num)]\n",
    "        num.sort()\n",
    "        return (num[0]+num[1])*10 + num[2] +num[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        s = [int(x) for x in str(num)]\n",
    "        s.sort()\n",
    "        t1 = t2 = \"\"\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            t1 += str(s[i])\n",
    "            i += 1\n",
    "            if i < n:\n",
    "                t2 += str(s[i])\n",
    "                i += 1\n",
    "        return int(t1) + int(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        digits = []\n",
    "        while num:\n",
    "            digits.append(num % 10)\n",
    "            num //= 10\n",
    "        digits.sort()\n",
    "        return 10 * (digits[0] + digits[1]) + digits[2] + digits[3]                       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        result = list(str(num))\n",
    "        result.sort()\n",
    "        return 10 * (int(result[0]) + int(result[1])) + int(result[2]) + int(result[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "\n",
    "        \n",
    "        _ = sorted([int(i) for i in str(num)])\n",
    "        return int(str(_[0]) + str(_[-1])) + int(str(_[1]) + str(_[-2]))\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        data_list = sorted(list(str(num)))\n",
    "        data_min = min(data_list[0] + data_list[2], data_list[2] + data_list[0])\n",
    "        data_min2 = min(data_list[1] + data_list[3], data_list[3] + data_list[1])\n",
    "        return int(data_min) + int(data_min2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        numbers = list(str(num))\n",
    "        numbers.sort()\n",
    "        i, j = 0, len(numbers) - 1\n",
    "        res = 0\n",
    "        while i <= j:\n",
    "            if i != j: res += int(numbers[i] + numbers[j])\n",
    "            else: res += int(numbers[i])\n",
    "            i, j = i + 1, j - 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumSum(self, num: int) -> int:\n",
    "        numlist = []\n",
    "        for i in range(4):\n",
    "            numlist.append(num%10)\n",
    "            num = num//10\n",
    "        numlist.sort()\n",
    "        return numlist[0] * 10 + numlist[1] * 10 + numlist[2] +numlist[3]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
